if {0} {
/*
 * This file is part of algowala.
 *
 * algowala is copyright (c) 2012 Jagannathan Sampath <algowala@jagernot.com>
 *
 * algowala is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * algowala is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with algowala.  If not, see <http://www.gnu.org/licenses/>.
 *
*/
}

package require Tk;

proc open-socket {host port} {  
  global sock_file
  set sock_file {}
  set sock_file [socket $host $port]
  fconfigure $sock_file -blocking 0 ;# non-blocking socket
}

proc puts-socket {what} {

  global sock_file sock_read
    puts $sock_file $what
  flush $sock_file
    
}

proc read-socket {{block 0}} {
  global sock_file
  set result {}
  while {$block ne 0} {
    set result [read $sock_file]
    if {$result ne ""} break
  }
  return $result
}

proc get-added-stock {} {
  global added_stock
  puts-socket list-stocks
  set added_stock [read-socket 1]
  puts "added stock: $added_stock"
}

proc add-stock {} {
  get-added-stock
  global symbol company added_stock
  set sel [.t.sym curselection] ;# get symbol & company
  foreach i $sel {
    global sock_file
    set sym [lindex $symbol $i]
    if {[lsearch -exact $added_stock $sym] eq -1} {
      set com [lindex $company $i]
      global params
      set cmd_args "$sym $com"
      foreach q $params {
        lappend cmd_args [.t.s$q get]
      }
      set cmd "add-stock $cmd_args"
      puts-socket $cmd
      puts $cmd
      
    }
  }
}

proc remove-stock {} {
  global added_stock
  set sel [.t.sym curselection]
  foreach i $sel {
    set sym [lindex $added_stock $i]
    puts-socket "remove-stock $sym"
  }
  load-added-stock  
}

proc scale-time {val} {
  puts-socket "set-var time_scale $val" 
}

proc read-securities {} {
  
  global securities 
  set f [open securities r]
  set securities [read $f]

  global symbol company
  foreach {i j} $securities {
    lappend symbol $i
    lappend company $j
  }
    
}

open-socket localhost 8150

read-securities

proc load-securities {} {
  
  .t.sym delete 0 end
  
  global securities
  foreach {i j} $securities {
    .t.sym insert end "$i - $j"
  }
  
}

proc load-added-stock {} {
  
  global added_stock symbol company
  get-added-stock
    
  .t.sym delete 0 end
  foreach i $added_stock {
    set j [lsearch $symbol $i]
    if {$j ne -1} {
      .t.sym insert end "$i - [lindex $company $j]"
    }
  }
  
}

catch [destroy .t]
toplevel .t
wm title .t algowala

proc set-fps {v} {
  puts-socket "set-var fps $v"
}

scale .t.stimescale -orient horizontal -label time_scale -from 1 -to 100 -command scale-time
scale .t.sfps -orient horizontal -label FPS -from 1 -to 120 -command set-fps

foreach {i j} { {get-var time_scale} .t.stimescale {get-var fps} .t.sfps} {
  puts-socket $i
  set v [read-socket 1]
  $j set $v
}

grid .t.sfps - - - -sticky news
grid .t.stimescale - - - -sticky news

listbox .t.sym -selectmode extended
grid .t.sym - - - -sticky news
grid rowconfigure .t .t.sym -weight 1
grid columnconfigure .t .t.sym -weight 1
bind .t.sym <<ListboxSelect>> list-selection-changed

proc list-selection-changed {} {
  
  global operation params edit_symbol added_stock sock_file
  
  foreach i $params {
    .t.s$i configure -command {}
  }
  
  if {$operation eq "edit stock"} {
    
    set sel [.t.sym curselection]
    set lensel [llength $sel]
    if {$lensel eq 0} return
    set top [lindex $sel 0]
    
    set edit_symbol [lindex $added_stock $top]
    puts-socket "get-stock $edit_symbol"
      set values [read-socket 1]
        puts $values
    
    set j 1
    foreach i $params {
      proc $i-changed val "puts-socket \"edit-stock $edit_symbol $i \$val\""
      set val [lindex $values [incr j]]
      set slider_name .t.s$i
      set to [$slider_name cget -to]
      if {$val > $to} {$slider_name configure -to $val}
      $slider_name set $val 
      $slider_name configure -command $i-changed
    }
    
  }
}

labelframe .t.f -text "operation"
radiobutton .t.f.r1 -text "add stock" -variable operation -value {add stock} -command operation-changed
radiobutton .t.f.r2 -text "edit stock" -variable operation -value {edit stock} -command operation-changed
grid .t.f - - - - -sticky news
grid .t.f.r1 - - .t.f.r2 - - -sticky news

proc operation-changed {args} {
  global operation
  if {$operation eq "add stock"} {
    load-securities
    catch [destroy .t.b1]
    catch [destroy .t.b2]
    button .t.ba
    .t.ba configure -text $operation -command add-stock
    grid .t.ba - - - - -sticky news -row 4 -column 0
  } else {
    load-added-stock
    catch [destroy .t.ba]
    button .t.b1
    button .t.b2
    .t.b1 configure -text "refresh stock" -command list-selection-changed
    .t.b2 configure -text "remove stock" -command remove-stock
    grid configure .t.b1 - -row 4 -column 0 -sticky we
    grid configure .t.b2 - -row 4 -column 2 -sticky we
  }
}

set operation {add stock}
operation-changed

set params {price price_volatility volume volume_volatility n_times m_seconds}
set from {0 0 1 1 1 1}
set to {500 100 10000 1000 100 100}
set res {0.01 0.01 1 1 1 0.1}
set values {100 1 250 5 10 1}

for {set i 0} {$i < 6} {incr i} {
  set param [lindex $params $i]
  scale .t.s$param -orient horizontal -label $param -from [lindex $from $i] -to [lindex $to $i] -resolution [lindex $res $i]
  .t.s$param set [lindex $values $i]
}

set j 7
foreach i $params {
  grid .t.s$i - - - -sticky news -row $j
  grid rowconfigure .t .t.s$i -weight 1
  grid columnconfigure .t .t.s$i -weight 1
  incr j
}

